	set more off

	
use Data/Estimation_sample.dta, clear
	
	gen nexis_buildings  = ROOFME/PROOFME
		
		
	gen roofratio = ROOFME/ROOFTI
	gen roofratiobuildings = roofratio*nexis_buildings
		
	gen roofrelativeIV = (roofratio*nexis_buildings/[moduleprice])/1000

	
	label variable TotalHouse "Dwellings (000)"
	label variable median_income "Median Income (000/week)"
	label variable median_age "Median Age"
	label variable PSeparate "Proportion Separate Dwellings"
	label variable median_rooms "Median House Size"
	label variable PFullTime "Proportion Employed Full Time"
	label variable median_mortgage "Median Mortgage Payment (000/month)"
	label variable median_rent "Median Rental Payment (000/week)"
	label variable PBachelor "Proportion Bachelor Degree"
	label variable PRented "Proportion Rental Property"

	
	

 
	xi: xtivreg2 green_p (cuminst = roofrelativeIV)  i.quarter  [aweight =NCustomers	], fe cluster(postcode) ffirst 

	
	quietly summ green_plan [aweight =NCustomers	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_IV, bdec(3) label ctitle("IV")  addstat(CDW F-test, e(widstat)) ///
	keep(cum median_*)  addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes replace 
 
 
 
 
	
	xi: xtivreg2 green_p (cuminst = roofrelativeIV) median_income_interp median_mortgage_interp PBachelor_interp PFullTime_interp  i.quarter  [aweight =NCustomers] , fe cluster(postcode) ffirst // 
	

	
	quietly summ green_plan [aweight =NCust	] if e(sample)
    local meandepvar=round(`r(mean)',.001)
	
	outreg2 using Analysis/FE_postcode_IV, bdec(3) label ctitle("IV")  addstat(CDW F-test, e(widstat)) ///
	keep(cum median_* P*)  addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append 
 
	
	label variable roofrelativeIV "Relative Roof IV"
	
	
	
	
	
	**** region by year FEs
	
	
	merge m:1 postcode using Data/SA3postcode.dta
	
	keep if _merge ==3
	drop _merge 
	gen SA3_year =  SA3 + string(year)
	
	quietly tab SA3_year, gen(SA3yearFE)
	
	
	xi: xtivreg2  green_p (cuminst =roofrelativeIV) SA3yearFE*  i.quarter [aweight =NCustomers	] , ///
	cluster(postcode) fe partial(SA3yearFE*)
	
	
	quietly summ green_plan [aweight =NCustomers	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	outreg2 using Analysis/FE_postcode_IV, bdec(3) label ctitle("IV")  addstat(CDW F-test, e(widstat)) ///
	keep(*cum* median_* P*)  addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append 
	
	
	
		**** roofratio  by year FEs

	
	merge m:1 postcode using Data/AboveBelowRoof.dta

	drop _merge

	merge m:1 postcode using Data/Postcode_matching.dta

	drop _merge 

	
	merge m:1 postcode using Data/SA3postcode.dta
	drop _merge
	merge m:1 SA3_CODE16 using "data/maps/sa3" 

	
	drop _merge
	*  melbourne

	gen Melbourne = 1 if SA4_CODE=="213" | SA4_CODE =="210" | SA4_CODE=="209" | SA4_CODE =="207" | SA4_CODE=="211"  | SA4_CODE=="208" | 	SA4_CODE=="206" | SA4_CODE=="212"
	replace Melbourne =0 if Melbourne ==.`'
				

	gen notinner25 = (Melbourne ==1 )*(GPO_distance>2.5)
	gen notinner5 = (Melbourne ==1 )*(GPO_distance>5)
	gen notinner75 = (Melbourne ==1 )*(GPO_distance>7.5)
	gen notinner10 = (Melbourne ==1 )*(GPO_distance>10)
				
				
			forvalues y = 2009(1)2015 { 
						forval q = 1(1)5{ 

			gen quin_year`y'_`q' = (year==`y')*(quintiles_Relative==`q')  
						}
			gen distance_year`y' = GPO_distance * year==`y'
 			gen above_med_`y' = (above_medRelative==2) * (year==`y')

			foreach var in 25 5 75 10 {
				gen notinneryear`var'_`y' = notinner`var'* (year==`y')
			
			
			}
			
					}
		
		
		
 
 
	xi: xtivreg2 green_p (cuminst = roofrelativeIV)  i.quarter distance_year* [aweight =NCustomers	], fe cluster(postcode) ffirst 

	
	
	quietly summ green_plan [aweight =NCustomers	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_IV, bdec(3) label ctitle("IV")  addstat(CDW F-test, e(widstat)) ///
	keep(cum median_*)  addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append 
 
 
 
 
 
 
 
 
	xi: xtivreg2 green_p (cuminst = roofrelativeIV)  i.quarter above_med_* [aweight =NCustomers	], fe cluster(postcode) ffirst 

	
	
	quietly summ green_plan [aweight =NCustomers	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_IV, bdec(3) label ctitle("IV")  addstat(CDW F-test, e(widstat)) ///
	keep(cum median_*)  addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append 
 
 
	  
		
	
	xi: xtivreg2 green_p (cuminst = roofrelativeIV)  i.quarter quin_year* [aweight =NCustomers	], fe cluster(postcode) ffirst 

	 
	 
	
	quietly summ green_plan [aweight =NCustomers	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_IV, bdec(3) label ctitle("IV")  addstat(CDW F-test, e(widstat)) ///
	keep(cum median_*)  addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append 
 
 
 

	
	
	merge m:1 postcode using Data/Census2006postcode.dta
	
	drop if _merge ==2
	drop _merge
	
	
 
 
	
	xi: ivreghdfe green_p (cuminst = roofrelativeIV)    [aweight =NCustomers] , absorb(postcode quarter c.median_income#i.year c.median_mortgage#i.year c.PBachelor#i.year c.PFullTime#i.year ) cluster(postcode) 
	

	
	quietly summ green_plan [aweight =NCustomers	] if e(sample)
	  local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_IV, bdec(3) label ctitle("IV")  addstat(CDW F-test, e(widstat)) ///
	keep(cum median_*)  addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append 
 

	
	
	
	
	
	
	
	
	
***********
	
	* Reduced form
	
		xi: xtivreg2 green_plan  roofrelativeIV  i.quarter  [aweight =NCustomers	], fe cluster(postcode) 
		
	
	quietly summ green_plan  [aweight =NCustomers	] if e(sample)
    local meandepvar=round(`r(mean)',.001)
		
	outreg2 using Analysis/RF_IVroofrelative_weightNCust, bdec(3) label ctitle("RF")  ///
	keep(roofrelativeIV median_* P*) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes replace 
	
	
	
	

	
		xi: xtivreg2 green_plan  roofrelativeIV  i.quarter median_income_interp median_mortgage_interp PBachelor_interp PFullTime_interp  [aweight =NCustomers	], fe cluster(postcode) 
		
		
	quietly summ green_plan  [aweight =NCustomers	] if e(sample)
    local meandepvar=round(`r(mean)',.001)
	
	outreg2 using Analysis/RF_IVroofrelative_weightNCust, bdec(3) label ctitle("RF")   ///
	keep(roofrelativeIV median_* P*) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append 

	
***********
	
	
	
	
	
